package com.bdth.mixapi.api.controller.index;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdth.mixapi.api.entity.IndustryOrder;
import com.bdth.mixapi.api.service.IndustryOrderService;
import com.bdth.mixapi.api.vo.CommonList;
import com.bdth.mixapi.api.vo.IndustryOrderVo;
import com.bdth.mixapi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-11
 */
@Api("按照行业用户量排序表")
@RestController
@RequestMapping("/index")
public class IndustryOrderController {
    @Autowired
    IndustryOrderService industryOrderService;

    @ApiOperation(value="获取行业用户量排名前10名")
    @RequestMapping(value = "industry/order10", method = RequestMethod.GET)
    public CommonList<IndustryOrderVo> getResult(){
        //根据日期倒叙排列，然后取第一个数据，就是当前最新的更新数据。
        QueryWrapper<IndustryOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("uptime");
        queryWrapper.last("limit 1");
        List<IndustryOrder> list = industryOrderService.list(queryWrapper);
        if(list!=null && list.size()>0){
            //统计数据
            Map<String,Object> query = new HashMap<>();
            System.out.println(list.get(0));
            query.put("uptime",DateUtil.format(list.get(0).getUptime()));
            List<IndustryOrderVo> result = industryOrderService.getIndustryTop10(query);
            return CommonList.success(result);
        }else{
            return CommonList.fail("没有数据");
        }
    }
}

